// @file main.c
//
//  date Created on: June 22, 2020
//  author meiguoqiang@inspur.com
//
//  Description:
//
//
//

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <string.h>


#include "svdpi.h"
#include <iostream>
#include <math.h>
#include <vector>

#include "engine.h"

int svbit_convert (int a )
{
	int b ;
	if (a>=32768) b = a-65536 ;
	else          b = a ;
	return b ;
}


int convert_svbit (int a )
{
	int b ;
	if (a>=32768)       b = 32767     ;
	else if (a<-32768)  b=-32768 ;
	else if (a<0)       b = a + 65536 ;
	else                b = a ;
	return b ;
}



extern "C"  int  fft_checker(svBitVecVal fftin_re[64],svBitVecVal fftin_im[64],svBitVecVal fftout_re[64],svBitVecVal  fftout_im[64], svBitVecVal oresult[1]) 
{
	printf("fft_checker run begin!\n");
	Engine *ep;
	mxArray *T1 = NULL;
	mxArray *T2 = NULL;
	mxArray *T3 = NULL;
	mxArray *T4 = NULL;
	mxArray *result = NULL;

	double  real_in[64];
	double  imag_in[64];
	double  real_out[64];
	double  imag_out[64];
	double  mresult[1];

	for(int i=0;i<64;i++) {
		real_in[i]=(double  (svbit_convert(fftin_re[i])/8192.0)) ;
		imag_in[i]=(double  (svbit_convert(fftin_im[i])/8192.0)) ;
		real_out[i]=(double (svbit_convert(fftout_re[i])/8192.0)) ;
		imag_out[i]=(double (svbit_convert(fftout_im[i])/8192.0)) ;
		printf("fftin_re[i] is %d  %d  %d  %d  %d   %f  %f %f  %f    \n",i,svbit_convert(fftin_re[i]),svbit_convert(fftin_im[i]),svbit_convert(fftout_re[i]),svbit_convert(fftout_im[i]),real_in[i],imag_in[i],real_out[i],imag_out[i]);

	}
		
    	/*
	 * Call engOpen with a NULL string. This starts a MATLAB process 
     * on the current host using the command "matlab".
	 */
	if (!(ep = engOpen(""))) {
		fprintf(stderr, "\nCan't start MATLAB engine\n");
		return EXIT_FAILURE;
	}

	/* 
	 * Create a variable for our data
	 */
	T1 = mxCreateDoubleMatrix(1, 64, mxREAL);
	T2 = mxCreateDoubleMatrix(1, 64, mxREAL);
	T3 = mxCreateDoubleMatrix(1, 64, mxREAL);
	T4 = mxCreateDoubleMatrix(1, 64, mxREAL);
	memcpy((void *)mxGetPr(T1), (void *)real_in, sizeof(real_in));
	memcpy((void *)mxGetPr(T2), (void *)imag_in, sizeof(imag_in));
	memcpy((void *)mxGetPr(T3), (void *)real_out, sizeof(real_out));
	memcpy((void *)mxGetPr(T4), (void *)imag_out, sizeof(imag_out));
	/*
	 * Place the variable T into the MATLAB workspace
	 */
	engPutVariable(ep, "T1", T1);
	engPutVariable(ep, "T2", T2);
	engPutVariable(ep, "T3", T3);
	engPutVariable(ep, "T4", T4);
	
    	/*
	 * Evaluate a function of time, distance = (1/2)g.*t.^2
	 * (g is the acceleration due to gravity)
	 */
	engEvalString(ep, "cd ../model");
	engEvalString(ep, "addpath(genpath('./'))");
	engEvalString(ep, "plot(T1);");
	engEvalString(ep, "mresult=fft_checker(T1,T2,T3,T4)");

	/*
	 * Get the variable result from the MATLAB workspace
	 */
	result=engGetVariable(ep, "mresult");
        memcpy((void*)mresult, (void*)mxGetPr(result), sizeof(mresult));

	//printf("Hit return to continue\n\n");
	//fgetc(stdin);
	
	for(int i=0;i<1;i++) {
		printf("mresult[%d] is  %f \n",i,mresult[i]);
	}

	oresult[0] = convert_svbit (mresult[0]*8192);
	printf("fft_checker run end!,the result is %f   %d \n",mresult[0],oresult[0]);
	engClose(ep); //关闭Matlab引擎，退出Matlab
	return 0 ;
}


int main(void)
{
	int time[64];
	int result;
	for(int i=0;i<64;i++)
		time[i]=i+1;
    		
    fft_checker(time,time,time,time,result);
    return result;
}
